"""Domain views.

TODO:
- Owners can only add/remove hcpMailAcceptingDomain
- domain admin can do anything
- hcpMailAcceptingDomain must be equal to or subdomain of hcpDomain

"""

from django import forms
from django.conf import settings
from hcp.auth.decorators import permission_required
from hcp.forms.custom import MyDynamicField, MyGroupedForm
from hcp.forms.grouped import FormSection
from hcp.forms.util import form_from_model
from hcp.jinja import render_to_response
from hcp.ldap_models import Domain, update_model_from_post
from hcp.tracking import HcpCallback
from hcp.util.json import JsonResponse

class DomainForm(MyGroupedForm):

    class Required(FormSection):

        label = 'Required Attributes'

        hcpDomainName = MyDynamicField(forms.CharField(), label='Domain Name')

    class Owner(FormSection):

        label = 'Owner'

        owner = MyDynamicField(forms.CharField(), label='Owner', required=False)

@permission_required('manage_domains')
def index(request):
    return render_to_response('domains/index.html', request=request)

@permission_required('manage_domains')
def domain_list(request):
    domains = Domain.objects.all()
    data = [ {'domain':domain.hcpDomainName[0],
              'owner':domain.owner,
              'dn':domain.dn} for domain in domains ]

    return JsonResponse({
        'domains': data,
    })

@permission_required('manage_domains')
def new(request):
    if request.method == 'POST':
        form = DomainForm(request.POST)

        if form.is_valid():
            domain = Domain()
            fields = form.fields.keys()
            update_model_from_post(domain, form.cleaned_data, fields)
            domain.save(callback=HcpCallback(request.user.uid[0]))
            # XXX: Handle errors.
            # XXX: Make this a dialog instead of a separate page.
            return render_to_response('domains/domain_created.html', {'domain': domain}, request=request)
    else:
        form = DomainForm()

    return render_to_response('domains/new.html',
        {'form': form,
        }, request=request)

@permission_required('manage_domains')
def edit(request, dn):
    domain = Domain.objects.get_dn(dn)
    # XXX not found

    if request.method == 'POST':
        form = DomainForm(request.POST)
        if form.is_valid():
            update_model_from_post(domain, form.cleaned_data, form.fields.keys())
            domain.save(callback=HcpCallback(request.user.uid[0]))
            # XXX: Handle errors.

            return render_to_response('domains/domain_edited.html', {'domain': domain}, request=request)
    else:
        form = form_from_model(DomainForm, domain)

    return render_to_response('domains/edit.html',
        {'domain': domain,
         'form': form,
        },
        request=request)

@permission_required('manage_domains')
def remove(request, dn):
    domain = Domain.objects.get_dn(dn)
    domain.delete(callback=HcpCallback(request.user.uid[0]))
    return JsonResponse({
        'result': 'success',
    })
